package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.Order;
import com.example.book_shop_java.dao.OrderDao;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class OrderController {
    @Resource
    OrderDao orderDao;

    @CrossOrigin
    @RequestMapping("/getAllOrder")
    public String getAllOrder(){
        List<Order> orders = orderDao.getAllOrder();
        HashMap<String, Object> res = new HashMap<>();
        String message = "success";
        int code = 200;
        res.put("code",code);
        res.put("data",orders);
        String allOrder = JSON.toJSONString(res);
        return allOrder;
    }

    @RequestMapping("/shipOrder")
    public String shipOrder(int order_id){
        int i = orderDao.shipOrder(order_id);
         int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }

        res.put("message",message);
        res.put("code",code);
        String shipOrderMessage = JSON.toJSONString(res);
        return shipOrderMessage;
    }

    @RequestMapping("/wxGetOrder")
    public String wxGetOrder(int wx_userid){
        List<Order> orders = orderDao.wxGetOrder(wx_userid);
        HashMap<String, Object> res = new HashMap<>();
        String message = "success";
        int code = 200;
        res.put("code",code);
        res.put("data",orders);
        String wxGetOrderMessage = JSON.toJSONString(res);
        return wxGetOrderMessage;
    }

    @RequestMapping("/sureGetGood")
    public String sureGetGood(int order_id){
        System.out.println(order_id);
        int i = orderDao.sureGetGood(order_id);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }

        res.put("message",message);
        res.put("code",code);
        String shipOrderMessage = JSON.toJSONString(res);
        return shipOrderMessage;
    }

    @RequestMapping("/getWitchBuy")
    public String getWitchBuy(int wx_userid){
        List<Order> orders = orderDao.getWitchBuy(wx_userid);
        HashMap<String, Object> res = new HashMap<>();
        String message = "success";
        int code = 200;
        res.put("code",code);
        res.put("data",orders);
        String wxGetOrderMessage = JSON.toJSONString(res);
        return wxGetOrderMessage;

    }
}
